<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('服务对象报餐记录列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>上报时间：</label>
                                <input type="text" class="time-input" placeholder="请选择上报时间" name="reportDate" th:value="${reportDate}"/>
                            </li>
                            <li>
                                <label>院区：</label>
                                <input type="text" name="campus" th:value="${campus}"/>
                            </li>
                            <li>
                                <label>班级：</label>
                                <input type="text" name="className" th:value="${className}"/>
                            </li>
                            <li>
                                <label>填报人：</label>
                                <input type="text" name="reportingPerson"  />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="meal:meal:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="meal:meal:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('meal:meal:edit')}]];
        var removeFlag = [[${@permission.hasPermi('meal:meal:remove')}]];
        var prefix = ctx + "meal/meal";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "服务对象报餐记录",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                onLoadSuccess:function(data) {
                // mergeTable(data,'bootstrap-table');
                    mergeCellsT(data, "reportDate", 1, '#bootstrap-table'); //行合并
                mergeCellsT(data, "className", 1, '#bootstrap-table'); //行合并
                mergeCellsT(data, "campus", 1, '#bootstrap-table'); //行合并
                mergeCellsT(data, "reportingPerson", 1, '#bootstrap-table'); //行合并
                // mergeRowspans('#bootstrap-table',0,['className','campus','reportingPerson'],4);
            },
                columns: [
                    [{
                        checkbox: true,
                        rowspan:2
                    },
                        {
                            field: 'id',
                            title: 'id',
                            visible: false,
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            field: 'campus',
                            title: '院区',
                            valign: 'middle',
                            rowspan:2
                        },{
                        field: 'className',
                        title: '班级',
                        valign: 'middle',
                        rowspan:2
                    },
                        {
                            field: 'reportDate',
                            title: '上报时间',
                            valign: 'middle',
                            rowspan:2
                        },

                        {
                            field: 'foodType',
                            title: '类别',
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            title : '早餐',
                            align : 'center',
                            colspan : 5
                        },
                            {
                                title : '午餐',
                                align : 'center',
                                colspan : 5
                            }, {
                                title : '晚餐',
                                align : 'center',
                                colspan : 5
                            },
                        {
                        field: 'reportingPerson',
                        title: '填报人',
                        valign: 'middle',
                        rowspan:2
                    }, {
                        title : '',
                        align : 'center',
                        colspan : 1
                    }
                            ],
                    [
                {
                    field: 'mOrphan',
                    title: '孤儿'
                },
                {
                    field: 'mNothing',
                    title: '事无'
                },
                {
                    field: 'mGove',
                    title: '政府'
                },
                {
                    field: 'mPoor',
                    title: '特困'
                },
                {
                    field: 'mExpense',
                    title: '自费'
                },
                {
                    field: 'lOrphan',
                    title: '孤儿'
                },
                {
                    field: 'lNothing',
                    title: '事无'
                },
                {
                    field: 'lGove',
                    title: '政府'
                },
                {
                    field: 'lPoor',
                    title: '特困'
                },
                {
                    field: 'lExpense',
                    title: '自费'
                },
                {
                    field: 'dOrphan',
                    title: '孤儿'
                },
                {
                    field: 'dNothing',
                    title: '事无'
                },
                {
                    field: 'dGove',
                    title: '政府'
                },
                {
                    field: 'dPoor',
                    title: '特困'
                },
                {
                    field: 'dExpense',
                    title: '自费'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]]
            };
            $.table.init(options);
        });

        /*报餐明细-详细*/
        function detail(mealId) {
            var url = prefix + '/detail/' + mealId;
            $.modal.openTab("报餐明细", url);
        }








        var firstSubjectCount="";
        var tegionCount="";

        /**
         * 合并行
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称数组
         * @param colspan 列数
         * @param target 目标表格对象
         */
        function mergeCellsT(data, fieldName, colspan, target) {
            if (data.length == 0) {
                alert("不能传入空数据");
                return;
            }
            var numArr = [];
            var value = data.rows[0][fieldName];
            console.log(data);
            var num = 0;
            for (var i = 0; i < data.rows.length; i++) {
                if (value != data.rows[i][fieldName]) {
                    numArr.push(num);
                    value = data.rows[i][fieldName];
                    num = 1;
                    continue;
                }if(i==data.rows.length-1 && value == data.rows[i][fieldName]){
                    num++;
                    numArr.push(num);
                    value = data.rows[i][fieldName];
                    continue;
                }
                num++;
            }
            var merIndex = 0;
            for (var i = 0; i < numArr.length; i++) {
                $(target).bootstrapTable('mergeCells', {
                    index: merIndex,
                    field: fieldName,
                    colspan: colspan,
                    rowspan: numArr[i]
                });
                merIndex += numArr[i];
            }
        }

        /**
         * 合并列
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性数组
         * @param target    目标表格对象
         */
        function mergeColspan(data, fieldNameArr, target) {
            if (data.length == 0) {
                alert("不能传入空数据");
                return;
            }
            if (fieldNameArr.length == 0) {
                alert("请传入属性值");
                return;
            }
            var num = -1;
            var index = 0;
            for (var i = 0; i < data.rows.length; i++) {
                num++;
                for (var v in fieldNameArr) {
                    index = 1;
                    if (data.rows[i][fieldNameArr[v]] != data.rows[i][fieldNameArr[0]]) {
                        index = 0;
                        break;
                    }
                }

                if (index == 0) {
                    continue;
                }

                $(target).bootstrapTable('mergeCells', {
                    index: num,
                    field: fieldNameArr[0],
                    colspan: fieldNameArr.length,
                    rowspan: 1
                });
            }
        }

        /**
         * 合并单元格
         * @param data 原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称
         * @param colspan  合并列
         * @param target  目标表格对象
         */
        function mergeCells(data, fieldName, colspan, target) {
            //声明一个map计算相同属性值在data对象出现的次数和
            var sortMap = {};
            for (var i = 0; i < data.length; i++) {
                for (var prop in data[i]) {
                    if (prop == fieldName) {
                        var key = data[i][prop]
                        if (sortMap.hasOwnProperty(key)) {
                            sortMap[key] = sortMap[key] * 1 + 1;
                        } else {
                            sortMap[key] = 1;
                        }
                        break;
                    }
                }
            }
            for (var prop in sortMap) {
                console.log(prop, sortMap[prop])
            }

            var index = 0;
            for (var prop in sortMap) {
                var count = sortMap[prop] * 1;
                $(target).bootstrapTable('mergeCells', {
                    index: index,
                    field: fieldName,
                    colspan: colspan,
                    rowspan: count
                });
                index += count;
            }
        }
    </script>
</body>
</html>